<?php


namespace app\admin\controller;

use app\admin\model\BalanceLogModel;
use app\admin\model\PersonModel;
use app\admin\model\ShopModel;
use app\admin\model\TaskQueueModel;
use app\common\Constants\ErrorCode;
use app\common\traits\ActionRecordTrait;
use app\crm\model\ShopPersonAchievementModel;

class Statistics extends ApiCommon
{
    use ActionRecordTrait;

    /**
     * 用于判断权限
     * @author ran
     * @date 2021-12-04 11:25
     * mailbox 466180170@qq.com
     */
    public function initialize()
    {
       $action = [
            'permission' => ['exportcsv','import'],
            'allow' => [
                'synckingdeestock'
            ]
        ];
        event('CheckAuth', $action);
        $request = $this->request;
        $a       = strtolower($request->action());
        if (!in_array($a, $action['permission'])) {
           parent::initialize();
        }
    }

    /**
     * 员工业绩统计
     * @author ran
     * @date 2021-12-04 11:27
     * mailbox 466180170@qq.com
     */
    public function readAdminShopPersonStatistics(){
        $request = $this->request->param();
        $shopIds = $this->getCommonShopIdsCollectByUserType();
        if(!empty($request['shopId'])){
            $shopIds=[$request['shopId']];
        }
        $model = new PersonModel();
//        if (empty($request['page']) || empty($request['limit'])) return $this->failed(ErrorCode::SYSTEM_INVALID);
        $res          = $model->getAdminShopPersonStatistics($shopIds,$request);
        return $this->success($res,'获取资源成功',$res['dataCount']);
    }

     /**
     * 资金日报表
     * @author ran
     * @date 2021-12-04 11:27
     * mailbox 466180170@qq.com
     */
    public function readAdminCapitalStatistics(){
        $request = $this->request->param();
        $shopIds = $this->getCommonShopIdsCollectByUserType();
        $user_id = $this->userInfo['id'];
        if($this->userInfo['group_id'] == 118)
        {
            $shopIds = $this->agentShopList($user_id);
        }
        //var_dump($shopIds);
        if(!empty($request['shopId'])){
            $shopIds=[$request['shopId']];
        }
        $model = new BalanceLogModel();
        if (empty($request['page']) || empty($request['limit'])) return $this->failed(ErrorCode::SYSTEM_INVALID);
        $res          = $model->getAdminShopCapitalStatistics($shopIds,(int)$request['page'],(int)$request['limit'],$request);
        return $this->success($res,'获取资源成功');
    }

    /**
     * 月度消耗报表
     * @author ran
     * @date 2021-12-04 11:27
     * mailbox 466180170@qq.com
     */
    public function readAdminShopStatistics(){
        $request = $this->request->param();
        $shopIds = $this->getCommonShopIdsCollectByUserType();
        $user_id = $this->userInfo['id'];
        if($this->userInfo['group_id'] == 118)
        {
            $shopIds = $this->agentShopList($user_id);
        }
        $model = new ShopModel();
        if (empty($request['page']) || empty($request['limit'])) return $this->failed(ErrorCode::SYSTEM_INVALID);
        $res          = $model->getAdminShopStatistics($shopIds,(int)$request['page'],(int)$request['limit'],$request);
        return $this->success($res,'获取资源成功');
    }
   /**
     * 月度业绩报表
     * @author ran
     * @date 2021-12-04 11:27
     * mailbox 466180170@qq.com
     */
    public function readAdminShopAchievementStatistics(){
        $request = $this->request->param();
        $shopIds = $this->getCommonShopIdsCollectByUserType();
        $user_id = $this->userInfo['id'];
        if($this->userInfo['group_id'] == 118)
        {
            $shopIds = $this->agentShopList($user_id);
        }
        $model = new ShopModel();
        if (empty($request['page']) || empty($request['limit'])) return $this->failed(ErrorCode::SYSTEM_INVALID);
        $res          = $model->readAdminShopAchievementStatistics($shopIds,(int)$request['page'],(int)$request['limit'],$request);
        return $this->success($res,'获取资源成功');
    }

    /**
     * 店铺任务导入模板
     *
     * @return \think\response\Json
     */
    public function getShopAchievementExcel()
    {
        $model = new ShopModel();
        $res   = $model->getShopAchievementExcel();
        return $this->success($res,'获取资源成功');
    }

     /**
     * 创建店铺任务
     * @author ran
     * @date 2021-12-04 11:27
     * mailbox 466180170@qq.com
     */
    public function shopTask(){
        $request = $this->request->param();
        $model = new PersonModel();
        $res          = $model->createShopTask($request);
        if (!$res) {
            return  $this->failed(ErrorCode::SYSTEM_INVALID,$model->getError());
        }
        return $this->success([],'创建资源成功');
    }

    /**
     * 批量导入门店任务
     *
     * @return \think\response\Json
     */

    public function importShopAchievementExcel()
    {
        $request = $this->request->param();
        $file = request()->file();
        $request['file']=$file['file'];
        $model = new PersonModel();
        $res          = $model->importShopAchievementExcel($request);
        if (!$res) {
            return  $this->failed(ErrorCode::SYSTEM_INVALID,$model->getError());
        }
        return $this->success([],'创建资源成功');

    }
    /**
     * 业绩达成统计
     * @author ran
     * @date 2021-12-04 11:27
     * mailbox 466180170@qq.com
     */
    public function sumThirdOneDayMoney(){
        $request = $this->request->param();
        $model = new PersonModel();
        $res          = $model->sumThirdOneDayMoney(3,170,'2018-06');
        return $this->success($res,'获取资源成功');
    }

    /**
     * 查看顾客复购率详情
     * @author ran
     * @date 2021-12-04 11:27
     * mailbox 466180170@qq.com
     */
    public function readAdminCustomerByShopId(){
        $request = $this->request->param();
        $model = new ShopModel();
        $res          = $model->readAdminCustomerByShopId((int)$request['shopId'],(int)$request['type'],(int)$request['page'],(int)$request['limit'],$request);
        return $this->success($res,'获取资源成功');
    }

	/**
	 * 查看店面业绩明细
	 * @author ran
	 * @date 2022-07-24 11:27
	 * mailbox 466180170@qq.com
	 */
	public function readAdminShopAmountList(){
	    $request = $this->request->param();
		$model = new ShopModel();
	    $res          = $model->readAdminShopAmountList((int)$request['shopId'],(int)$request['type'],(int)$request['page'],(int)$request['limit'],$request);
	    return $this->success($res,'获取资源成功');
	}

	/**
	 * 查看新单补单的明细
	 * @author ran
	 * @date 2022-07-24 11:27
	 * mailbox 466180170@qq.com
	 */
	public function readAdminNewTypeList(){
		$request = $this->request->param();
		$model = new ShopModel();

		$res          = $model->readAdminNewTypeList((int)$request['shopId'],(int)$request['type'],(int)$request['page'],(int)$request['limit'],$request);
		return $this->success($res,'获取资源成功');
	}

    /**
     * 任务队列
     *
     * @return \think\response\Json
     */

    public function addTaskQueue()
    {
        $request = $this->request->param();
        $shopIds = $this->getCommonShopIdsCollectByUserType();
        if(!empty($request['shopId'])){
            $shopIds=[$request['shopId']];
        }
        if(empty($shopIds))
        {
            return $this->failed(300,"门店为空");
        }
        $shopIds = implode(",",$shopIds);
        $taskQueue = new TaskQueueModel();
        $taskQueue->task = $request['task'];
        $taskQueue->shop_id = $shopIds;
        $res = $taskQueue->save();
        if($res)
        {
            return $this->success(['id'=>$taskQueue->id],'添加任务队列成功');
        }else{
            return $this->failed(400,"添加任务队列失败");
        }
    }

    /**
     * 修改员工业绩提成
     *
     * @return \think\response\Json
     */
    public function updatePersonCommission(){
        $request = $this->request->param();
        $id = $request['id'];
        $achievement = ShopPersonAchievementModel::find($id);
        if(!$achievement)
        {
            return $this->failed(400,"未找到对应记录");
        }else{
            $achievement->commission = $request['commission']??0;
            $achievement->save();
            return $this->success(['id'=>$achievement->id],'修改成功');

        }

    }

    /**
     * 任务状态
     *
     * @return \think\response\Json
     */
    public function getTaskQueueStatus()
    {
        $request = $this->request->param();
        $task  = $request['task'];
        $taskQueue = TaskQueueModel::where('task','=',$task)->order('id desc')->find();
        $msg="可执行";
        $code = 200;
        switch ($taskQueue->status)
        {
            case 0:{
                $code = 0;
                $msg = "待执行";
                break;
            }
            case 1:{
                $code = 1;
                $msg = "正在执行";
                break;
            }
            case 2:{
                $code = 2;
                $msg = "可执行";
                break;
            }
        }
        return $this->success(['status'=>$code],$msg);
    }


}
